package mesweb.mesweb.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.MesActionTherbligs;
import mesweb.mesweb.service.impl.MesActionTherbligsServiceImpl;
import mesweb.mesweb.vo.MesActionTherbligsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */
@RestController
@RequestMapping("/mes/mes-action-therbligs")
@Api(tags = "动素管理", description = "MesActionTherbligsController")
public class MesActionTherbligsController {
    @Autowired
    private MesActionTherbligsServiceImpl mesActionTherbligsService;

    @GetMapping("/GetActionTherbligss")
    @ApiOperation("获取设备列表")
    public CommonResult<IPage<MesActionTherbligsVo>> GetActionTherbligss(@ApiParam("设备名称") @RequestParam(value = "name", required = false) Integer name,
                                                                         @ApiParam("当前页") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                         @ApiParam("每页行数") @RequestParam(value = "size", required = false, defaultValue = "6") Integer size) {
        QueryWrapper<MesActionTherbligsVo> queryWrapper = new QueryWrapper<>();
        if (name > 0) {
            queryWrapper.eq("a.id", name);
        }
        Page<MesActionTherbligsVo> mesActionTherbligsVoPage = new Page<>(page, size);
        IPage<MesActionTherbligsVo> data = mesActionTherbligsService.getActionTherbligs(mesActionTherbligsVoPage, queryWrapper);
        return CommonResult.success(data);
    }

    @GetMapping("/AllActionTherbligss")
    @ApiOperation("获取所有设备列表")
    public CommonResult<List<MesActionTherbligs>> AllActionTherbligss() {
        List<MesActionTherbligs> data = new ArrayList<>();
        data = mesActionTherbligsService.list();
        return CommonResult.success(data);
    }

    @GetMapping("/GetActionTherbligs")
    @ApiOperation("根据编号获取设备")
    public CommonResult<MesActionTherbligs> GetActionTherbligsByNo(@ApiParam("设备编号") @RequestParam("no") String no) {
        if (no.isEmpty())
            return null;
        QueryWrapper<MesActionTherbligs> queryWrapper = new QueryWrapper<MesActionTherbligs>();
        queryWrapper.eq("ActionTherbligs_Type_No", no);
        return CommonResult.success(mesActionTherbligsService.getOne(queryWrapper));
    }

    @PostMapping("/SaveActionTherbligs")
    @ApiOperation("保存设备")
    public CommonResult<Boolean> SaveActionTherbligs(@ApiParam("设备") @RequestBody MesActionTherbligs mesActionTherbligs) {
        if (mesActionTherbligs == null)
            return null;

        try {
            boolean result = false;
            if (mesActionTherbligs.id == 0L) {
                QueryWrapper<MesActionTherbligs> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("Therbligs_No", mesActionTherbligs.getTherbligsNo());
                queryWrapper.or().eq("Therbligs_Name", mesActionTherbligs.getTherbligsName());
                MesActionTherbligs mesTechnologyFlow1 = mesActionTherbligsService.getOne(queryWrapper);
                if (mesTechnologyFlow1 != null) {
                    String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getTherbligsNo(), mesTechnologyFlow1.getTherbligsName());
                    throw new ApiException(msg);
                }
                result = mesActionTherbligsService.save(mesActionTherbligs);
            } else {
                result = mesActionTherbligsService.updateById(mesActionTherbligs);
            }
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }

    @DeleteMapping("/deleteBatch")
    @ApiOperation("删除设备")
    public CommonResult<Boolean> DeleteActionTherbligs(@ApiParam("设备ID") @RequestBody List<Long> ids) {
        try {
            boolean result = mesActionTherbligsService.removeByIds(ids);
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }
}

